package com.neo.base.actorTest2

import scala.actors.Actor._
import scala.actors.TIMEOUT

/**
  * Created by hadoop on 6/15/17.
  */
object ReceiveWithin {
  def main(args: Array[String]): Unit = {
    val caller = self

    val accumulator = actor {
      var sum = 0
      var continue = true
      while (continue) {
        sum += receiveWithin(1000) {
          case number : Int => number
          case TIMEOUT =>
            println("Timed out! Will return result now")
            continue = false
            0
        }
      }

      caller ! sum
    }


    accumulator ! 1
    accumulator ! 7
    accumulator ! 8

    receiveWithin(10000) { case result => println("Total is " + result) }
  }
}
